#ifndef _LULYUI_BUTTON_H_
#define _LULYUI_BUTTON_H_
#include <functional>

#include "../NinePatch.h"
#include "Layout.h"
#include "TextView.h"

class Button : public UIObject {
 protected:
  virtual void init() override;
  virtual void event() override;
  virtual void update() override;
  virtual void draw() override;
  virtual void drawArea(const Rect& area) override;
  TextView* textView;
  Layout* layout;
  NinePatch* defualtNinePatch;
  NinePatch* hoveringNinePatch;
  NinePatch* clickedNinePatch;
  NinePatch* currentNinePatch;
  bool isButtonDown = false;
  void* fingerid = nullptr;

  //是否可以取消点击
  //取消点击意思是
  //在不小心按下的时候，可以通过保持鼠标点击移向别处，然后松开鼠标，这样也不会触发onClicked事件
  //如果为false，那么，在移动到别处的时候，会自动弹起并触发onClicked事件
  bool couldCancel = true;

  //事件回调函数指针

  // void (*onClick_ptr)() = nullptr;
  // void (*onHovering_ptr)() = nullptr;
  // void (*onButtonDown_ptr)() = nullptr;
  // void (*onButtonUp_ptr)() = nullptr;

  std::function<void()> onClicked_callback = []() {};
  std::function<void()> onHovering_callback = []() {};
  std::function<void()> onButtonDown_callback = []() {};
  std::function<void()> onButtonUp_callback = []() {};
  std::function<void()> onButtonHolding_callback = []() {};

 public:
  Button(std::string uiObjectId, std::string text, int x = 0, int y = 0);
  virtual void onClicked(std::function<void()> onClicked) {
    onClicked_callback = onClicked;
  }
  virtual void onHovering(std::function<void()> onHovering) {
    onHovering_callback = onHovering;
  }
  virtual void onButtonDown(std::function<void()> onButtonDown) {
    onButtonDown_callback = onButtonDown;
  }
  virtual void onButtonUp(std::function<void()> onButtonUp) {
    onButtonUp_callback = onButtonUp;
  }
  virtual void onButtonHolding(std::function<void()> onButtonHolding) {
    onButtonHolding_callback = onButtonHolding;
  }

  void setWidth(int w) override {
    UIObject::setWidth(w);
    layout->setWidth(w - currentNinePatch->getContentToLeft() -
                     currentNinePatch->getContentToRight());
  }

  void setHeight(int h) override {
    UIObject::setHeight(h);
    layout->setHeight(h - currentNinePatch->getContentToBottom() -
                      currentNinePatch->getContentToTop());
  }

  void setHeightMode(WHMode hm) override {
    UIObject::setHeightMode(hm);
    layout->setHeightMode(hm);
  }
  void setWidthMode(WHMode wm) override {
    UIObject::setWidthMode(wm);
    layout->setWidthMode(wm);
  }
  void setWHMode(WHMode wm, WHMode hm) override {
    UIObject::setHeightMode(hm);
    layout->setHeightMode(hm);
    UIObject::setWidthMode(wm);
    layout->setWidthMode(wm);
  }

  void setWHMode(WHMode m) override {
    UIObject::setWHMode(m);
    layout->setWHMode(m);
  }

  void setCouldCancel(bool couldOrNot) { couldCancel = couldOrNot; }

  const std::string& getTextStr() { return textView->getTextStr(); }
  ~Button();
};

#endif